package com.tensquare.base.controller;

import com.tensquare.base.pojo.City;
import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.CityService;
import com.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/label")
@CrossOrigin
public class UserController {

    @Autowired
    private LabelService labelService;
    @Autowired
    private CityService cityService;

    //查询 城市
    @RequestMapping("cityList")
    public List<City> cityList(){
        return cityService.clist();
    }

    /**
     * 根据 使用数量 查询 热门标签
     * @return
     */
    @RequestMapping(value = "/findLabelAll",method= RequestMethod.GET)
    public Result findLabelAll(){
        return new Result(true,StatusCode.OK,"查询成功",labelService.findByRecommendOrderByCountDesc());
    }
    /**
     * 根据 id 关注 标签
     * @return/label/addFans/{id}
     */
    @RequestMapping(value = "/addFans/{id}",method= RequestMethod.PUT)
    public Result addFans(@PathVariable String id){
        labelService.addFans(id);
        System.out.println("调用。");
        return new Result(true,StatusCode.OK,"关注成功","");
    }
    /**
     * 查询全部数据
     * @return
     */
    @RequestMapping(value="/",method=RequestMethod.GET)
    public List<Label> findAll(){
        return labelService.findAll();
    }

    /**
     * 根据ID查询
     * @param id ID
     * @return
     */
    @RequestMapping(value="/{id}",method=RequestMethod.GET)
    public Result findOne(@PathVariable String id){
        System.out.println("No.2");
        return new Result(true,StatusCode.OK,"查询成功",labelService.findOne(id) );
    }

    /**
     * 分页查询全部数据
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value="/{page}/{size}",method=RequestMethod.GET)
    public PageResult<Label> findPage(@PathVariable int page, @PathVariable int size){
        Page<Label> pageList = labelService.findPage(page, size);
        return new PageResult<Label>(pageList.getTotalElements(), pageList.getContent());
    }

    /**
     * 分页+多条件查询
     * @param searchMap 查询条件封装
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @RequestMapping(value="/{page}/{size}",method=RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
        Page<Label> pageList = labelService.findSearch(searchMap, page, size);
        return new Result(true,StatusCode.OK,"查询成功!",new PageResult<>(pageList.getTotalElements(),pageList.getContent()));
    }

    /**
     * 增加
     * @param label
     */
    @RequestMapping(value="/",method=RequestMethod.POST)
    public Result add(@RequestBody Label label  ){

        labelService.add(label);

        return new Result(true,0,"");
    }

    /**
     * 修改
     * @param label
     */
    @RequestMapping(value="/{id}",method=RequestMethod.PUT)
    public Result update(@RequestBody Label label,@PathVariable String id ){
        label.setId(id);
        labelService.update(label);
        return new Result(true,0,"");
    }

    /**
     * 删除
     * @param id
     */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public Result delete(@PathVariable String id ){
        labelService.delete(id);
        return new Result(true,0,"");
    }
}
